package com.jeethink.project.cadre.controller;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jeethink.common.utils.poi.ExcelUtil;
import com.jeethink.common.utils.security.ShiroUtils;
import com.jeethink.framework.aspectj.lang.annotation.Log;
import com.jeethink.framework.aspectj.lang.enums.BusinessType;
import com.jeethink.framework.web.controller.BaseController;
import com.jeethink.framework.web.domain.AjaxResult;
import com.jeethink.framework.web.page.TableDataInfo;
import com.jeethink.project.system.dept.service.IDeptService;
import com.jeethink.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jeethink.project.cadre.domain.SysUserCadre;
import com.jeethink.project.cadre.service.ISysUserCadreService;

/**
 * 用户信息Controller
 * 
 * @author xyp
 * @date 2020-09-21
 */
@Controller
@RequestMapping("/cadre/cadre")
public class SysUserCadreController extends BaseController
{
    private String prefix = "cadre";

    @Autowired
    private ISysUserCadreService sysUserCadreService;

    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("cadre:cadre:view")
    @GetMapping()
    public String cadre()
    {
        return prefix + "/cadre";
    }

    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("cadre:cadre:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUserCadre sysUserCadre)
    {
        startPage();
        List<SysUserCadre> list = sysUserCadreService.selectSysUserCadreList(sysUserCadre);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @RequiresPermissions("cadre:cadre:export")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysUserCadre sysUserCadre,String ids)
    {
        sysUserCadre.setLoginIp(ids);

        List<SysUserCadre> list = sysUserCadreService.selectSysUserCadreList(sysUserCadre);
        ExcelUtil<SysUserCadre> util = new ExcelUtil<SysUserCadre>(SysUserCadre.class);
        return util.exportExcel(list, "cadre");
    }

    /**
     * 新增用户信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户信息
     */
    @RequiresPermissions("cadre:cadre:add")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUserCadre sysUserCadre)
    {
        String birthday = sysUserCadre.getBirthday();
        int age =  sumAge(birthday);
        sysUserCadre.setAge(age);
        return toAjax(sysUserCadreService.insertSysUserCadre(sysUserCadre));
    }

    /**
     * 修改用户信息
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        SysUserCadre sysUserCadre = sysUserCadreService.selectSysUserCadreById(userId);
        mmap.put("sysUserCadre", sysUserCadre);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户信息
     */
    @RequiresPermissions("cadre:cadre:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysUserCadre sysUserCadre)
    {

        String birthday = sysUserCadre.getBirthday();
        int age =  sumAge(birthday);
        sysUserCadre.setAge(age);
        return toAjax(sysUserCadreService.updateSysUserCadre(sysUserCadre));
    }

    /**
     * 删除用户信息
     */
    @RequiresPermissions("cadre:cadre:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysUserCadreService.deleteSysUserCadreByIds(ids));
    }


    @Scheduled(cron = "0 0 1 1 * ?")
    public void updateAge(){
        System.out.print("开始执行");
        List<SysUserCadre> list = sysUserCadreService.selectAgeList();
        if(list !=null && list.size()>0){
            for(SysUserCadre a : list){
                SysUserCadre userCadre = new SysUserCadre();
                String birthDay = a.getBirthday();
                int age = sumAge(birthDay);
                userCadre.setAge(age);
                userCadre.setUserId(a.getUserId());
                sysUserCadreService.updateAge(userCadre);
            }
        }
    }

    /**
     * 计算年龄
     * @param
     * @return
     * @throws Exception
     */
    private  int sumAge(String csny){
        //根据出生年月求年龄
        //根据出生年月求年龄     适用于 2018-1-1   类似的日期
        //解析出生年月
        String[] split = csny.split("-");
        if(split.length != 3){
            //给的出生年月不合法
            return -1;
        }
        //求当前年月日
        Calendar now = Calendar.getInstance();
        int year  =  now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1;
        int day =  now.get(Calendar.DAY_OF_MONTH);
        //算出当前大概年龄
        int age = year - Integer.parseInt(split[0]);
        if(age <0){
            //能计算出负数，怕是没出生吧
            return -1;
        }
        if(month - Integer.parseInt(split[1]) > 0){
            //过生日了
        }else if(month - Integer.parseInt(split[1]) == 0){
            //在本月，不知道有没有过生日，需要用天去判断
            if(day - Integer.parseInt(split[2]) < 0){
                //没有过生日
                age -= 1;
            }
        }else{
            //没有过生日
            age -= 1;
        }
        return age;
    }

    @RequiresPermissions("cadre:cadre:list")
    @PostMapping("/getSexCount")
    @ResponseBody
    public JSONObject getSexCount(SysUserCadre sysUserCadre)
    {
        startPage();
        User user = ShiroUtils.getSysUser();
        sysUserCadre.setDeptId(user.getDeptId());
        JSONObject list = sysUserCadreService.selectSexCount(sysUserCadre);
        // 当前登录用户

        return list;
    }

}
